import 'dart:io';

import 'package:fimber/fimber.dart';
import 'package:flex_color_scheme/flex_color_scheme.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';
import 'package:xim/screen/login_screen/login_guide_page/login_guide_page_binding.dart';
import 'common/store/global.dart';
import 'config/theme_config.dart';
import 'screen/login_screen/login_guide_page/login_guide_page.dart';

final InAppLocalhostServer localhostServer =
    InAppLocalhostServer(documentRoot: 'assets');

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  if (!kIsWeb) {
    Fimber.i("start the localhost server");
    await localhostServer.start();
  }
  if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
    await InAppWebViewController.setWebContentsDebuggingEnabled(kDebugMode);
  }
  Fimber.plantTree(DebugTree());
  runApp(const MyApp());
  Fimber.plantTree(DebugTree.elapsed());
}

class MyApp extends StatefulWidget {
  const MyApp({super.key});

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> with WidgetsBindingObserver {
  final _focusNode = FocusNode();

  /// 是否启用屏保
  bool get isEnableScreenSecure => true;

  /// 默认首页
  Widget get home => GestureDetector(
        child: LoginGuidePage(),
      );

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
        designSize: const Size(360, 690),
        builder: (BuildContext context, Widget? child) {
          GlobalStore.instance.updateBrightness(context: context);
          return GetMaterialApp(
            themeMode: ThemeMode.system,
            theme: ThemeConfig.theme,
            darkTheme: ThemeConfig.darkTheme,
            initialBinding: LoginGuidePageBinding(),
            locale: const Locale('zh', 'CN'),
            supportedLocales: const [
              Locale('en', "US"),
              Locale('zh', "CN"),
            ],
            localizationsDelegates: const [
              GlobalMaterialLocalizations.delegate,
              GlobalWidgetsLocalizations.delegate,
              GlobalCupertinoLocalizations.delegate,
            ],
            title: "TrackMap",
            debugShowCheckedModeBanner: false,
            builder: (ctx, child) => FlutterEasyLoading(
              child: child,
            ),
            home: home,
          );
        });
  }

  @override
  void initState() {
    super.initState();
    if (Platform.isAndroid) {
      SystemUiOverlayStyle style = const SystemUiOverlayStyle(
          statusBarColor: Colors.transparent,
          statusBarIconBrightness: Brightness.dark);
      SystemChrome.setSystemUIOverlayStyle(style);
    }
  }

  @override
  void dispose() {
    super.dispose();
    GlobalStore.instance.dispose();
    _focusNode.dispose();
  }
}
